<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2020/10/14
 * Time: 17:24
 */

namespace app\astrologer\controller;

use app\common\cachemodel\ZlcrAstrMessage;
use Exception;
use think\Db;

/**
 * 消息中心
 * Class Message
 * @package app\astrologer\controller
 */
class Message extends AstrAuth
{
    /**
     * 消息中心
     * @throws Exception
     */
    public function index()
    {
        $page = input('page/d', 1);

        $data = (new ZlcrAstrMessage())->getMessageList($this->astrInfo['id']);

        if ($page === 1)
        {
            (new ZlcrAstrMessage())->messageReadAll($this->astrInfo['id']);// 消息全部已读
        }

        return show(200, '请求成功', $data);
    }

    /**
     * 消息已读
     * @throws Exception
     */
    public function msgRead()
    {
        $id = input('id/d', 0);

        $messageModel = new ZlcrAstrMessage();

        $messageInfo = $messageModel->getInfoById($id, ['astr_id']);

        if (!$messageInfo || (int)$messageInfo['astr_id'] !== $this->astrInfo['id'])
        {
            return show(config('code.error'), '服务异常，消息不存在');
        }

        $res = $messageModel->messageRead($id);

        if ($res)
        {
            return show(config('code.success'), '请求成功');
        }

        return show(config('code.error'), '服务异常，请稍后再试～');
    }
}
